﻿using System;
using System.Linq;
using System.Security;
using Peak.Core.DataInterfaces;
using System.Security.Principal;
using Peak.Core;
using SharpArch.Core.PersistenceSupport;

namespace Peak.ApplicationServices.Services
{
  public class SprintStoryService : ISprintStoryService
  {
    readonly IRepository<SprintStory> _sprintStoryRepository;
    private readonly IPrincipal _principal;

    public SprintStoryService(IRepository<SprintStory> sprintStoryRepository, IPrincipal principal)
    {
      _sprintStoryRepository = sprintStoryRepository;
      _principal = principal;
    }

    public SprintStory Get(int id)
    {
      SprintStory sprintStory = _sprintStoryRepository.Get(id);
      if(sprintStory == null || sprintStory.Story.Project.Users.FirstOrDefault(f=>f.UserName.ToLower()== _principal.Identity.Name.ToLower()) == null)
      {
        throw new EntityNotFoundException();
      }
      return sprintStory;
    }    
  }
}
